﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using UM_SERVER.Command;
using UM_SERVER.Net;
using UM_SERVER.Packet;
using UM_SERVER.Session;
using UM_SERVER.Triggers.TriggerObject;
using System.Collections;

namespace UM_SERVER
{
    class Program
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            program.Init();
        }

        private void Init()
        {
            reSetColor();
            PrintHeader();
            Console.Write("\r\n服务器初始化中...");
            Console.Write("\r\n正在获取配置信息...");
            try
            {
                int Port = int.Parse(Globle.CI.GetXmlContent("../Config/Config.xml", "ServerConfig", "ListenPort"));
                if (Port <= 0)
                {
                    throw new Exception();
                }
                AppendSucess();
                Console.Write("\r\n正在打开端口 : " + Port + "...");
                //创建消息监听器
                SystemUdpMessageListener sysListener = new SystemUdpMessageListener(3072);
                sysListener.OnGetMessage += new EventHandler<MessageListener.OnGetMessageEventArgs>(sysListener_OnGetMessage);
                //创建端口监听器
                SystemUdpPortListener udpListener = new SystemUdpPortListener(Port, sysListener);
                //安装端口监听器
                if (Globle.udpPortListenerManager.InstallListener(udpListener))
                {
                    //成功
                    AppendSucess();
                    Console.Write("\r\n正在初始化包解析器...");
                    Globle.packageHelper = new PackageHelper();
                    AppendSucess();
                    Console.Write("\r\n正在安装包解析器...");
                    Globle.packageHelper.InstallParser(Protocol.NetworkProtocol.TALK_MESSAGE, new TalkMessageProtocolParser());
                    AppendSucess();
                    Console.Write("\r\n正在安装包处理器...");
                    Globle.processorRouter.InstallProcessor(new BasePacketListener());
                    Globle.processorRouter.InstallProcessor(new TalkMessagePacketListener());
                    AppendSucess();
                    Console.Write("\r\n正在安装命令解析器...");
                    Globle.commandHelper = new CommandHelper();
                    AppendSucess();
                    Console.Write("\r\n正在创建命令类型...");
                    AppendSucess();
                    Console.Write("\r\n正在安装命令处理器...");
                    Globle.commandHelper = new CommandHelper();
                    Globle.commandProcessRouter.InstallCommandListener(new BaseCommandListener());
                    AppendSucess();
                    Console.Write("\r\n正在初始化会话管理器...");
                    Globle.sessionManager = new SessionManager();
                    AppendSucess();
                    Console.Write("\r\n正在初始化超时管理器...");
                    Globle.timeoutTimerManager = new UM_SERVER.Timer.TimeoutTimerManager();
                    AppendSucess();
                    Console.Write("\r\n正在初始化触发器管理器...");
                    Globle.triggerManager = new UM_SERVER.Triggers.TriggerManager();
                    AppendSucess();
                    Console.Write("\r\n正在添加触发器...");
                    AppendText("WAIT", ConsoleColor.Yellow);
                    Console.Write("\r\nInitialize For User Online Trigger...");
                    UserOnlineTrigger userOnlineTrigger = new UserOnlineTrigger("UserOnlineTrigger");
                    Globle.triggerManager.Add(userOnlineTrigger);
                    AppendSucess();
                    Console.Write("\r\nInitialize For Delete Friend Trigger...");
                    DeleteFriendTrigger deleteFriendTrigger = new DeleteFriendTrigger("DeleteFriendTrigger");
                    Globle.triggerManager.Add(deleteFriendTrigger);
                    AppendSucess();
                    Console.Write("\r\n\r\n初始化服务器成功, 等待用户连接中...");
                    Console.WriteLine();
                    Console.WriteLine();
                    InputCommand.NotifyCommand();
                }
                else
                {
                    //失败
                    throw new Exception();
                }
            }
            catch (Exception ex)
            {
                AppendFail();
                Stop();
            }
        }

        void sysListener_OnGetMessage(object sender, MessageListener.OnGetMessageEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void PrintHeader()
        {
            Console.WriteLine("****************************************************************************");
            Console.WriteLine("*   The Server of Hi' Baidu Talking Service                                  ");
            Console.WriteLine("*   Prowered by: 剑走偏锋 on machine: UMBRELLA-5D2C5            ");
            Console.WriteLine("****************************************************************************");
            Console.WriteLine();
        }

        private void AppendSucess()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("SUCESS");
            reSetColor();
        }

        private void AppendFail()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("FAIL");
            reSetColor();
        }

        private void AppendText(String Text, ConsoleColor Color)
        {
            Console.ForegroundColor = Color;
            Console.Write(Text);
            reSetColor();
        }

        private void reSetColor()
        {
            Console.ForegroundColor = ConsoleColor.White;
        }

        private void Stop()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("\r\n\r\n服务器启动失败...");
            Console.ReadLine();
        }
    }
}
